package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@Controller
@ResponseBody
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/dish/page")
    public ResultInfo findByPage(@RequestParam(value = "page") Integer pageNum, Integer pageSize, String name) {
        Page<Dish> page = dishService.findByPage(pageNum, pageSize, name);
        return ResultInfo.success(page);
    }

    //新增菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish) {
        dishService.save(dish);

        return ResultInfo.success(null);
    }

    //修改菜品
    @GetMapping("dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id) {
        Dish dish = dishService.findById(id);

        return ResultInfo.success(dish);
    }

    @PutMapping("/dish")
    public ResultInfo updateDish(@RequestBody Dish dish) {
        dishService.updateDish(dish);
        return ResultInfo.success(null);
    }

    //停售
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateState(@PathVariable("status") int status, Long[] ids) {
        return dishService.updateDishStatus(status, ids);
    }

    @DeleteMapping("/dish")
    public ResultInfo deleteFish(String[] ids) {
        return dishService.deleteDish(ids);
    }

    //根据分类id查询菜品列表
    @GetMapping("/dish/list")
    public ResultInfo findByCategoryId(Long categoryId) {
        List<Dish> dishList = (List<Dish>) dishService.findByCategoryId(categoryId);

        return ResultInfo.success(dishList);
    }

    @GetMapping(params = "name", value = "/dish/list")
    public ResultInfo findByName(String name) {

        return ResultInfo.success(dishService.findByName(name));
    }


}
